package com.ktwlsoft.archivemanage.controller.businessInspect;

import com.ktwlsoft.archivemanage.domain.business.repositoryService.businessInspect.CommentRepository;
import com.ktwlsoft.archivemanage.domain.dao.entity.businessInspect.Comment;
import com.ktwlsoft.archivemanage.payload.businessGuidance.CommentRequest;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 1：数据库未能正常插入新数据，因为返回的int不大于0
 */
@Api(description = "评论")
@RequestMapping("/comment")
@RestController
public class CommentController {

    @Autowired
    private CommentRepository commentRepository;

    @ApiOperation("新增领导评论")
    @PostMapping("/insertLeaderComment")
    @UserAuthenticate(actionCode = "BUSINESS_GUIDANCE_B_LEAD_COM")
    public ResponseEntity<?> insertLeaderComment(@Valid @RequestBody CommentRequest request){
        request.setLeader(true);
        int row = commentRepository.saveComment(request);
        if(row>0){
            return ResponseEntity.ok(Result.ok("保存成功！"));
        }else{
            return ResponseEntity.ok(Result.body("未能正常保存，错误代码：1", ResultType.BUSINESS_ERROR));
        }
    }

    @ApiOperation("新增评论")
    @PostMapping("/insert")
    public ResponseEntity<?> insertComment(@Valid @RequestBody CommentRequest request){
        request.setLeader(false);
        int row = commentRepository.saveComment(request);
        if(row>0){
            return ResponseEntity.ok(Result.ok("保存成功！"));
        }else{
            return ResponseEntity.ok(Result.body("未能正常保存，错误代码：1", ResultType.BUSINESS_ERROR));
        }
    }

    @ApiOperation("编辑评论")
    @PostMapping("/update")
    public ResponseEntity<?> updateComment(@RequestBody CommentRequest request){
        int row = commentRepository.updateComment(request);
        if(row>0){
            return ResponseEntity.ok(Result.ok("编辑成功！"));
        }else{
            return ResponseEntity.ok(Result.body("未能正常保存，错误代码：1", ResultType.BUSINESS_ERROR));
        }
    }

    @ApiModelProperty("删除评论")
    @GetMapping("/delete")
    public ResponseEntity<?> deleteComment(@RequestParam Long id){
        int row = commentRepository.delete(id);
        if(row>0){
            return ResponseEntity.ok(Result.ok("删除成功！"));
        }else{
            return ResponseEntity.ok(Result.body("未能正常保存，错误代码：1", ResultType.BUSINESS_ERROR));
        }
    }

    @ApiOperation("获取所有评论")
    @GetMapping("/getAll")
    public ResponseEntity<?> selectAll(@RequestParam Long guidanceId){
        List<Comment> comments = commentRepository.getAll(guidanceId);
        return ResponseEntity.ok(Result.ok(comments));
    }
}
